#include "renderer.h"

#include <stdio.h>

void CEF_CALLBACK on_before_command_line_processing_render(struct _cef_app_t *self, const cef_string_t *process_type,
                                                           struct _cef_command_line_t *command_line) {
    // 打印进程类型
    if (process_type) {
        cef_string_utf8_t cef_cmd_line_utf8 = {};
        cef_string_utf16_to_utf8(process_type->str, process_type->length, &cef_cmd_line_utf8);
        LOG_INFO("Process type: %s\n", cef_cmd_line_utf8.str);
        cef_string_utf8_clear(&cef_cmd_line_utf8);
    } else {
        LOG_DEBUG("Process type: Browser\n");
    }

    // // 修改命令行参数（例如，禁用GPU）
    // cef_string_t sintch1 = {};
    // cef_string_utf8_to_utf16("disable-gpu", strlen("disable-gpu"), &sintch1);
    // cef_string_t sintch2 = {};
    // cef_string_utf8_to_utf16("disable-software-rasterizer", strlen("disable-software-rasterizer"), &sintch2);
    // cef_string_t sintch3 = {};
    // cef_string_utf8_to_utf16("disable-gpu-compositing", strlen("disable-gpu-compositing"), &sintch3);
    // cef_string_t sintch4 = {};
    // cef_string_utf8_to_utf16("in-process-gpu", strlen("in-process-gpu"), &sintch4);
    // command_line->append_switch(command_line, &sintch1);
    // command_line->append_switch(command_line, &sintch2);
    // command_line->append_switch(command_line, &sintch3);
    // command_line->append_switch(command_line, &sintch4);

    // cef_string_clear(&sintch1);
    // cef_string_clear(&sintch2);
    // cef_string_clear(&sintch3);
    // cef_string_clear(&sintch4);

    // // 修改命令行参数（完全禁用GPU）
    // const char *disable_switches[] = {
    //     "disable-gpu",
    //     "disable-software-rasterizer",
    //     "disable-gpu-compositing",
    //     "disable-gpu-early-init", // 新增：阻止早期初始化
    //     "disable-gpu-sandbox",    // 新增：解除沙盒限制
    //     "use-gl=disabled"         // 新增：强制禁用GL
    // };

    // const char *disable_switches[] = {"disable-gpu"};

    // for (int i = 0; i < sizeof(disable_switches) / sizeof(disable_switches[0]); i++) {
    //     cef_string_t switch_str = {};
    //     const char *switch_name = disable_switches[i];
    //     cef_string_utf8_to_utf16(switch_name, strlen(switch_name), &switch_str);
    //     command_line->append_switch(command_line, &switch_str);
    //     cef_string_clear(&switch_str);
    // }
}

struct _cef_render_process_handler_t *CEF_CALLBACK get_render_process_handler(struct _cef_app_t *self) {
    renderer_app *app = (renderer_app *)self;
    return &app->renderer_process_handler;
}

void CEF_CALLBACK on_register_custom_schemes_render(struct _cef_app_t *self,
                                                    struct _cef_scheme_registrar_t *registrar) {}

struct _cef_resource_bundle_handler_t *CEF_CALLBACK get_resource_bundle_handler_render(struct _cef_app_t *self) {

    // 返回NULL表示使用默认的资源包加载方式
    return NULL;
}

void CEF_CALLBACK on_context_created(struct _cef_render_process_handler_t *self, struct _cef_browser_t *browser,
                                     struct _cef_frame_t *frame, struct _cef_v8_context_t *context) {
    bind_ipc_renderer(context);
}

// 处理主进程消息
int CEF_CALLBACK on_process_message_received_render(struct _cef_render_process_handler_t *self,
                                                    struct _cef_browser_t *browser, struct _cef_frame_t *frame,
                                                    cef_process_id_t source_process,
                                                    struct _cef_process_message_t *message) {
    return handle_main_to_renderer_msg(browser, frame, message);
}

void CEF_CALLBACK on_context_released(struct _cef_render_process_handler_t *self, struct _cef_browser_t *browser,
                                      struct _cef_frame_t *frame, struct _cef_v8_context_t *context) {
    LOG_DEBUG("on_context_released renderer........................");
    // hashmap_free(ipcRenderer_handler_map);
    // hashmap_free(ipcRenderer_callback_map);
}

void CEF_CALLBACK on_browser_destroyed(struct _cef_render_process_handler_t *self, struct _cef_browser_t *browser) {
    LOG_DEBUG("on_browser_destroyed renderer........................");
    hashmap_free(ipcRenderer_handler_map);
    hashmap_free(ipcRenderer_callback_map);
}

renderer_app *renderer_app_create_t() {
    renderer_app *app = calloc(1, sizeof(renderer_app));

    app->renderer_process_handler.base.size = sizeof(cef_render_process_handler_t);
    app->renderer_process_handler.on_context_created = on_context_created;
    app->renderer_process_handler.on_process_message_received = on_process_message_received_render;
    app->renderer_process_handler.on_browser_destroyed = on_browser_destroyed;

    app->cef_app.base.size = sizeof(cef_app_t);
    app->cef_app.get_render_process_handler = get_render_process_handler;
    app->cef_app.on_before_command_line_processing = on_before_command_line_processing_render;
    app->cef_app.on_register_custom_schemes = on_register_custom_schemes_render;
    app->cef_app.get_resource_bundle_handler = get_resource_bundle_handler_render;

    return app;
}